package com.tac.service.wms.impl;

import com.tac.common.result.Result;
import com.tac.dao.wms.MaterialDao;
import com.tac.dao.wms.MaterialTypeDao;
import com.tac.pojo.wms.Material;
import com.tac.pojo.wms.MaterialType;
import com.tac.service.wms.MaterialTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialTypeServiceImpl implements MaterialTypeService {

    @Autowired
    private MaterialTypeDao materialTypeDao;

    @Autowired
    private MaterialDao materialDao;
    //查看分类列表
    public Result<List<MaterialType>> getList(Integer parentId){
        List<MaterialType> list = materialTypeDao.getList(parentId);
        return Result.success(list);
    }
    //修改物料分类
    public Result update(MaterialType materialType){
        Integer integer = materialTypeDao.update(materialType);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("修改物料分类失败");
    }
    //添加物料分类
    public Result save(MaterialType materialType){
        Integer integer = materialTypeDao.save(materialType);
        if(integer>0){
            MaterialType byId = materialTypeDao.getById(materialType.getTypeId());
            return Result.success(byId);
        }
        return Result.error("添加物料分类失败");
    }
    //删除物料分类
    public Result remove(List<Integer> ids){
        //删除应该有很多限制、如分类下有物料、分类下有子分类。
        for(Integer id:ids){
            List<MaterialType> list = materialTypeDao.getList(id);
            if(list.size()!=0){
                return Result.error("分类下有子分类不能删除");
            }
            List<Material> list1 = materialDao.getList(id, null, null, null, null);
            if(list1.size()!=0){
                return Result.error("分类下有物料不能删除");
            }
        }
        Integer integer = materialTypeDao.remove(ids);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("删除物料分类失败");
    }
}
